﻿using Newtonsoft.Json;
using PlayListSet.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PlayListSet.Comm
{
    internal static  class AppHelp
    {
        public enum CopyType
        {
            PlayInfoItem,PlayCmd
        }
        public class ATSCopyClipboardData
        {
            public const string DataFromat = "PlayListSetComm";
            public CopyType CopyType { get; set; }
            public object Data { get; set; }
        }
        /// <summary>
        /// 序列化对象并复制到剪贴板
        /// </summary>
        /// <param name="obj"></param>
        public static void CopyToClipboard(ATSCopyClipboardData data)
        {
            string jsonData = JsonConvert.SerializeObject(data);  // 美化输出
                                                                  //Clipboard.SetText(jsonData);
         
            Clipboard.SetData(ATSCopyClipboardData.DataFromat, jsonData);

        }

        /// <summary>
        /// 从剪贴板读取并反序列化为指定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ATSCopyClipboardData PasteFromClipboard()
        {

            var json = Clipboard.GetData(ATSCopyClipboardData.DataFromat);
          
            var data = Newtonsoft.Json.JsonConvert.DeserializeObject<ATSCopyClipboardData>(json + "");
            return data;

        }

        private static bool appSqlSugarClientInit = false;
        /// <summary>
        /// 应用数据库
        /// </summary>
        /// <returns></returns>
        public static SqlSugar.SqlSugarClient GetAppSqlSugarClient()
        {
            string dbFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"cfg", "appcfg.db");
            //string connStr = new SqliteConnectionStringBuilder()
            //{
            //    DataSource = dbName,
            //    Mode = SqliteOpenMode.ReadWriteCreate
            //                        ,
            //    Password = "admin"
            //}.ToString();
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = DbType.Sqlite,
                ConnectionString = $"DataSource={dbFile}",
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                MoreSettings = new ConnMoreSettings()
                {
                    SqliteCodeFirstEnableDefaultValue = true //启用默认值
                } 
                ,
             });
            if (!appSqlSugarClientInit)
            {
                lock ("appSqlSugarClientInit")
                {
                    if (!appSqlSugarClientInit)
                    {
                        appSqlSugarClientInit = true;
                        if (!System.IO.File.Exists(dbFile))
                        {
                            //建库：如果不存在创建数据库存在不会重复创建 createdb
                            db.DbMaintenance.CreateDatabase(); // 注意 ：Oracle和个别国产库需不支持该方法，需要手动建库 
                                                               //   File.Create(dbFile);
                        }
                        //创建表：根据实体类CodeFirstTable1  (所有数据库都支持)                    
                        db.CodeFirst.
                            InitTables(typeof(AppConfig), typeof(ChannelInfo), typeof(PlayCmd),typeof(PlayInfo),typeof(UserPlayInfoCmd));

                    }
                }
            }
            db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前
            {
                //获取原生SQL推荐 5.1.4.63  性能OK
                //UtilMethods.GetNativeSql(sql,pars)

                //获取无参数化SQL 影响性能只适合调试
                //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
                Console.WriteLine(sql);
            };
            db.Aop.OnLogExecuted = (sql, pars) =>
            {
                //我可以在这里面写逻辑

                //技巧：AOP中获取IOC对象
                //var serviceBuilder = services.BuildServiceProvider();
                //var log= serviceBuilder.GetService<ILogger<WeatherForecastController>>();
            };
            return db;
        }
        public static string GetGuid()
        {
            Guid uuid = Guid.NewGuid();
            string strUuid = uuid.ToString();
            return strUuid.Replace("-", "");
        }
    }

}
